﻿using System;

namespace Kernelx.Contexts.Defaults
{

    /// <summary>
    ///
    /// </summary>
    public interface IJsonSerializer
    { 

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        string SerializeObject(object obj, Type type);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="json"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        object DeserializeObject(string json, Type type);

    }

    /// <summary>
    /// 
    /// </summary>
    public static  class JsonSerializerExtensions
    {

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonSerializer"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
       public static string SerializeObject<T>(this IJsonSerializer jsonSerializer, T obj) where T : class
            => jsonSerializer.SerializeObject(obj,typeof(T));

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jsonSerializer"></param>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T DeserializeObject<T>(this IJsonSerializer jsonSerializer, string json) where T : class
            => jsonSerializer.DeserializeObject(json,typeof(T)) as T;
         
    }

}
